package cn.bs.busuo.content.service.impl;


import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import cn.bs.busuo.common.pojo.authentication.CurrentPrincipal;
import cn.bs.busuo.content.dao.persist.repository.HotelArticleRepository;
import cn.bs.busuo.content.pojo.entity.ContentHotelArticle;
import cn.bs.busuo.content.pojo.entity.ContentHotelArticleDetail;
import cn.bs.busuo.content.pojo.param.HotelArticleAddNewParam;
import cn.bs.busuo.content.pojo.vo.ContentHotelArticleListBySortVO;
import cn.bs.busuo.content.service.HotelArticleService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class HotelArticleServiceImpl implements HotelArticleService {

    @Autowired
    private HotelArticleRepository hotelArticle;

    //发布文章
    @Override
    public void addArticle(CurrentPrincipal currentPrincipal, HotelArticleAddNewParam hotelArticleAddNewParam) {
        //插入到酒店文章
        ContentHotelArticle contentHotelArticle = new ContentHotelArticle();
        contentHotelArticle.setBrief(hotelArticleAddNewParam.getBrief());
        contentHotelArticle.setCoverUrl(hotelArticleAddNewParam.getCoverUrl());
        contentHotelArticle.setHotelsId(hotelArticleAddNewParam.getHotelsId());
        contentHotelArticle.setKeywords(hotelArticleAddNewParam.getKeywords());
        contentHotelArticle.setTitle(hotelArticleAddNewParam.getTitle());

        //插入当事人信息
        contentHotelArticle.setAuthorId(currentPrincipal.getId());
        contentHotelArticle.setAuthorName(currentPrincipal.getUsername());

        //插入到酒店详情实体类
        ContentHotelArticleDetail contentHotelArticleDetail = new ContentHotelArticleDetail();
        contentHotelArticleDetail.setDetail(hotelArticleAddNewParam.getDetail());

        //调用添加方法
        addHotelArticle(contentHotelArticleDetail,contentHotelArticle);
    }




    //删除文章
    @Override
    public void deleteArticle(List<Long> ids) {
        deleteHotelArticle(ids);
    }

    //推荐文章
    @Override
    public void setIsRecommendHotelArticle(List<Long> ids) {
        for (Long id : ids){
            if (hotelArticle.selectHotelArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有id为: " + id + "这篇文章");
            }
            if (hotelArticle.contentHotelArticleById(id).getIsRecommend() == 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是推荐文章");
            }
            if (hotelArticle.contentHotelArticleById(id).getCheckState()!= 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"文章未审核");
            }
        }
//        ContentHotelArticle contentHotelArticle = hotelArticle.contentHotelArticleById(ids);
//        //判断文章是否存在
//        if (contentHotelArticle == null){
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
//        }
//
//        //判断是否已经是推荐
//        if (contentHotelArticle.getIsRecommend() == 1){
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是推荐文章");
//        }
//        if (contentHotelArticle.getCheckState()!= 1){
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"文章未审核");
//        }
        int i = hotelArticle.updateIsRecommend(ids, 1);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"推荐文章失败,服务器繁忙,请稍后再试");
        }

    }

    //不推荐文章
    @Override
    public void setIsNotRecommendHotelArticle(List<Long> ids) {
        for (Long id : ids){
            if (hotelArticle.selectHotelArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if(hotelArticle.contentHotelArticleById(id).getIsRecommend() == 2){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是不推荐文章");
            }
        }

//        ContentHotelArticle contentHotelArticle = hotelArticle.contentHotelArticleById(ids);
//        //判断文章是否存在
//        if (contentHotelArticle == null){
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
//        }
//
//        //判断是否已经是不推荐
//        if (contentHotelArticle.getIsRecommend() == 2){
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经是不推荐文章");
//        }
        int i = hotelArticle.updateIsNotRecommend(ids, 2);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"取消推荐文章失败,服务器繁忙,请稍后再试");
        }
    }

    //审核通过
    @Override
    public void setCheckStatePass(List<Long> ids) {
        for (Long id : ids){
            if (hotelArticle.selectHotelArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if (hotelArticle.contentHotelArticleById(id).getCheckState() == 1){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经审核通过");
            }
        }
//        ContentHotelArticle contentHotelArticle = hotelArticle.contentHotelArticleById(ids);
//        //判断文章是否存在
//        if (contentHotelArticle == null){
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
//        }
//
//        //判断是否审核通过
//        if (contentHotelArticle.getCheckState() == 1){
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经审核通过");
//        }
            int i = hotelArticle.updateCheckState(ids, 1);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"审核文章失败,服务器繁忙,请稍后再试");
        }

    }

    //审核为不通过
    @Override
    public void setCheckStateNotPass(List<Long> ids) {

        for (Long id : ids){
            if (hotelArticle.selectHotelArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
            if((hotelArticle.contentHotelArticleById(id).getCheckState() == 2)){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该文章已审核");
            }
        }
//        ContentHotelArticle contentHotelArticle = hotelArticle.contentHotelArticleById(id);
//        //判断文章是否存在
//        if (contentHotelArticle == null){
//            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
//        }
//        //判断是否审核通过
//        if ((contentHotelArticle.getCheckState() == 1) || (contentHotelArticle.getCheckState() == 2)){
//            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该文章已审核");
//        }

        int i = hotelArticle.updateCheckNotState(ids, 2);
        if (i == 0){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"审核文章失败,服务器繁忙,请稍后再试");
        }

    }

    //设置排序
    @Override
    public void setSort(Long id,Integer sort) {
        ContentHotelArticle contentHotelArticle = hotelArticle.contentHotelArticleById(id);
        //判断文章是否存在
        if (contentHotelArticle == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有这篇文章");
        }
        //判断是否通过审核
        if ((contentHotelArticle.getCheckState() == 0) || (contentHotelArticle.getCheckState() == 2)){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该文章还未通过审核");
        }
        //判断设置的排序与数据库里面的是否重复
        List<ContentHotelArticleListBySortVO> contentHotelArticlesList = hotelArticle.selectListBySort();
        for (ContentHotelArticleListBySortVO contentHotelArticleListBySortVO : contentHotelArticlesList) {
            if (contentHotelArticleListBySortVO.getSort() == sort){
                throw new ServiceException(ServiceCode.ERROR_CONFLICT,"已经存在该排序");
            }

        }
        int i = hotelArticle.updateSort(id,sort);
        if (i != 1){
            throw new ServiceException(ServiceCode.ERROR_DELETE,"设置排序失败,服务器繁忙,请稍后再试");
        }
    }

    //查看排序文章
    @Override
    public String listSortArticle() {
        List<ContentHotelArticleListBySortVO> contentHotelArticlesList = hotelArticle.selectListBySort();
        String json = JSON.toJSON(contentHotelArticlesList).toString();
        return json;
    }

    //查看未审核文章
    @Override
    public String listCheckStateArticle() {
        List<ContentHotelArticle> contentHotelArticlesList = hotelArticle.selectListByCheckState();
        if (contentHotelArticlesList.size() == 0 ){
            throw new ServiceException(ServiceCode.OK,"暂时还没有未审核的酒店文章");
        }
        String json = JSON.toJSON(contentHotelArticlesList).toString();
        return json;
    }







    //--------------方法类


    //添加酒店文章IO
    @Transactional
    public void addHotelArticle(ContentHotelArticleDetail contentHotelArticleDetail, ContentHotelArticle contentHotelArticle) {
        //判断该文章标题是否重复
        int title = hotelArticle.contentHotelArticleByTitle(contentHotelArticle.getTitle());
        if (title != 0) {
            String msg = "该文章标题已存在,请重新输入";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        //添加到酒店文章
        int i = hotelArticle.addNewHotelArticle(contentHotelArticle);
        if (i != 1){
            String msg = "发布酒店文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }

        //从酒店文章里面获取文章Id
        Long NewArticleId = hotelArticle.selectHotelArticleByTitle(contentHotelArticle.getTitle());
        contentHotelArticleDetail.setHotelArticleId(NewArticleId);

        //添加到酒店文章详情
        int j = hotelArticle.addNewHotelArticleDetail(contentHotelArticleDetail);
        if (j != 1){
            String msg = "发布酒店文章失败,服务器繁忙,请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }

    //删除酒店文章IO
    @Transactional
    public void deleteHotelArticle(List<Long> ids) {
        for (Long id : ids){
            if (hotelArticle.selectHotelArticleById(id) == 0){
                throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"没有" + id + "这篇文章");
            }
        }
//        //判断该文章是否存在
//       int i = hotelArticle.selectHotelArticleById(id);
//       if (i == 0){
//           String msg = "该文章不存在";
//           throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
//       }

       //删除酒店文章
        int j = hotelArticle.deleteHotelArticle(ids);
        if (j == 0){
           String msg = "删除酒店文章失败,服务器繁忙,请稍后再试";
           throw new ServiceException(ServiceCode.ERROR_DELETE,msg);
       }

    }
}